package com.wyw.hemerocallis.handler;

import com.alibaba.fastjson.JSON;
import com.wyw.hemerocallis.vo.JwtToken;
import com.wyw.hemerocallis.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;

/**
 * Sentinel 回退降级的兜底策略
 * 都需要静态方法,否则Sentinel会发现不了
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
public class WywFallbackHandler {

    /**
     * getTokenFromAuthorityService 方法的 fallback
     * */
    public static JwtToken getTokenFromAuthorityServiceFallback(
            UsernameAndPassword usernameAndPassword
    ) {
        log.error("get token from authority service fallback: [{}]",
                JSON.toJSONString(usernameAndPassword));
        return new JwtToken("wyw-fallback");
    }

    /**
     * 让Sentinel 忽略的异常
     * ignoreException 方法的 fallback
     * */
    public static JwtToken ignoreExceptionFallback(String code) {
        log.error("ignore exception input code: [{}] has trigger exception", code);
        return new JwtToken("wyw-fallback");
    }

}
